package com.hermes.biz.common.util.json;

import java.lang.reflect.Field;
import java.util.List;

import com.hermes.biz.common.constants.AllowedReturnFiledForJson;
import com.hermes.biz.common.constants.CommonSplitCodeConstants;

public class JsonDataParserUtil {

	
	public  static String parse(Object o) {
		
		StringBuffer sb = new StringBuffer("{");
		
		Field[] fields = o.getClass().getDeclaredFields();
		int size = fields.length;
		Field.setAccessible(fields,true);

		for(Field field :fields ){
			
			size--;
			String fieldName = field.getName();
			
			if(!AllowedReturnFiledForJson.isAllowedField(fieldName)){
				continue;
			}
			
				Object fieldValue;
				try {
					fieldValue = field.get(o);
					if(size>0){
						sb.append(fieldName).append(CommonSplitCodeConstants.COLON).append(fieldValue==null?CommonSplitCodeConstants.BLANK:fieldValue.toString()).append(";");
					}
					else{
						sb.append(fieldName).append(CommonSplitCodeConstants.COLON).append(fieldValue==null?CommonSplitCodeConstants.BLANK:fieldValue.toString());
					}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			
		}
		
		sb.append("}");
		
		return sb.toString();
	}
	
	public static <T> String parseList(List<T> list){
		
		StringBuffer sb = new StringBuffer("{");		
		int size = list.size();
		for(Object o:list){
			size--;
			sb.append(parse(o));
			if(size>0){
				sb.append(CommonSplitCodeConstants.COMMA);
			}
		}
		sb.append("}");
		return sb.toString();
	}
	
}
